import { http } from '../utils/request.js'

// 获取活跃房间列表
export const getRoomList = () => {
  return http.get('/rooms')
}

// 创建房间
export const createRoom = (data) => {
  return http.post('/rooms', {
    roomName: data.roomName,
    playerNickname: data.playerNickname,
    maxPlayers: data.maxPlayers || 4,
    gameDuration: data.gameDuration || 2
  })
}

// 获取房间详情
export const getRoomInfo = (roomId) => {
  // 对 404 进行静默处理，避免全局错误弹窗
  return http.get(`/rooms/${roomId}`, {}, { silent: true, silentCodes: [404] })
}

// 加入房间
export const joinRoom = (roomId) => {
  // 后端通过登录态识别玩家，无需提交昵称，提交多余字段可能被判定为无效参数
  return http.post(`/rooms/${roomId}/join`, {}, { silent: true, silentCodes: [400, 404] })
}

// 离开房间
export const leaveRoom = (roomId) => {
  return http.post(`/rooms/${roomId}/leave`)
}

// 开始游戏
export const startGame = (roomId) => {
  return http.post(`/rooms/${roomId}/start`)
}

// 结束游戏
export const endGame = (roomId) => {
  return http.post(`/rooms/${roomId}/end`)
}

// 删除房间
export const deleteRoom = (roomId) => {
  return http.delete(`/rooms/${roomId}`)
}

// 统一导出房间API
export const roomAPI = {
  getRoomList,
  createRoom,
  getRoomInfo,
  joinRoom,
  leaveRoom,
  startGame,
  endGame,
  deleteRoom
}
